package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Out;
import com.zyd.shiro.business.service.PakOutService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakOut;
import com.zyd.shiro.persistence.mapper.PakOutMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@Service
public class PakOutServiceImpl implements PakOutService {

    @Autowired
    private PakOutMapper pakOutMapper;

    @Override
    public Out insert(Out out) {
        Assert.notNull(out, "对象不可为空！");
        out.getPakOut().setUpdateTime(new Date());
        out.getPakOut().setCreateTime(new Date());
        pakOutMapper.insertSelective(out.getPakOut());
        return out;
    }

    @Override
    public void insertList(List<Out> outs) {
            for(Out out:outs){
               out.getPakOut().setUpdateTime(new Date());
               out.getPakOut().setCreateTime(new Date());
               pakOutMapper.insertSelective(out.getPakOut());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakOutMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Out out) {
        Assert.notNull(out, "对象不可为空！");
        out.setUpdateTime(new Date());
        return pakOutMapper.updateByPrimaryKey(out.getPakOut()) > 0;
    }

    @Override
    public boolean updateSelective(Out out) {
        Assert.notNull(out, "对象不可为空！");
        out.setUpdateTime(new Date());
        return pakOutMapper.updateByPrimaryKeySelective(out.getPakOut()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Out getByPrimaryKey(Long primaryKey) {
        PakOut pakOut = pakOutMapper.selectByPrimaryKey(primaryKey);
        return null == pakOut ? null : new Out(pakOut);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Out getOneByEntity(Out test) {
        Assert.notNull(test, "对象不可为空！");
        PakOut pakOut = pakOutMapper.selectOne(test.getPakOut());
        return null == pakOut ? null : new Out(pakOut);
    }

    @Override
    public List<Out> listAll() {
        List<PakOut> pakOuts = pakOutMapper.selectAll();
        if (CollectionUtils.isEmpty(pakOuts)) {
            return null;
        }
        List<Out> outs = new ArrayList<>();
        for (PakOut pakOut : pakOuts) {
            outs.add(new Out(pakOut));
        }
        return outs;
    }

    @Override
    public List<Out> listByEntity(Out entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Out> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakOut> pakOuts = pakOutMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakOuts)) {
            return null;
        }
        List<Out> outs = new ArrayList<>();
        for (PakOut pakOut : pakOuts) {
            outs.add(new Out(pakOut));
        }
        PageInfo bean = new PageInfo<PakOut>(pakOuts);
        bean.setList(outs);
        return bean;
    }
@Override
    public void increase(PakOut pakOut) {
        pakOutMapper.insert(pakOut);
    }




}
